package com.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.mapper.*;
import com.wms.pojo.*;
import com.wms.service.WarehousingEntryUpService;
import com.wms.vo.CorrectWarehousingUpVo;
import com.wms.vo.WarehousingEntryUpVo;
import com.wms.vo.WarehousingUpVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yzxxn
 */
@Service
public class WarehousingEntryUpServiceImpl extends ServiceImpl<WarehousingEntryUpMapper, WarehousingEntryUp> implements WarehousingEntryUpService {

    @Resource
    private WarehousingEntryUpMapper warehousingEntryUpMapper;
    @Resource
    private WarehouseMapper warehouseMapper;
    @Resource
    private ItemSkuMapper itemSkuMapper;
    @Resource
    private WarehouseAreaMapper warehouseAreaMapper;
    @Resource
    private WarehouseAreaLocalMapper warehouseAreaLocalMapper;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, String supplierName) {
        PageHelper.startPage(page, pageSize);
        List<WarehousingUpVo> list = getBaseMapper().queryPage(supplierName);
        Page<WarehousingUpVo> upVoPage = (Page<WarehousingUpVo>) list;
        return new PageResult(upVoPage.getTotal(), upVoPage.getResult());
    }

    @Override
    public PageResult selectList(Integer page, Integer pageSize, String warehouseName, String skuName) {
        PageHelper.startPage(page, pageSize);
        List<CorrectWarehousingUpVo> list = getBaseMapper().getSelectList(warehouseName, skuName);
        Page<CorrectWarehousingUpVo> voPage = (Page<CorrectWarehousingUpVo>) list;
        return new PageResult(voPage.getTotal(), voPage.getResult());
    }

    @Override
    public PageResult getWarehousingEntryUpByPage(String title, String code, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WarehousingEntryUp> warehousingEntryUps = warehousingEntryUpMapper.selectList(
                Wrappers.<WarehousingEntryUp>lambdaQuery().notIn(WarehousingEntryUp::getCount, 0));
        Page<WarehousingEntryUp> p = (Page<WarehousingEntryUp>) warehousingEntryUps;

        List<WarehousingEntryUpVo> vos = new ArrayList<>();
        p.getResult().forEach(warehousingEntryUp -> {
            ItemSku itemSku = itemSkuMapper.selectOne(Wrappers.<ItemSku>lambdaQuery()
                    .eq(ObjectUtil.isNotEmpty(warehousingEntryUp.getSkuId()), ItemSku::getId, warehousingEntryUp.getSkuId())
                    .like(ObjectUtil.isNotEmpty(title), ItemSku::getTitle, title)
                    .like(ObjectUtil.isNotEmpty(code), ItemSku::getCode, code));

            if (itemSku != null) {
                WarehousingEntryUpVo vo = BeanUtil.toBean(itemSku, WarehousingEntryUpVo.class);
                vo.setId(Long.valueOf(warehousingEntryUp.getId()));
                vo.setSkuId(Long.valueOf(itemSku.getId()));
                vo.setSkuTitle(itemSku.getTitle());
                vo.setCount(warehousingEntryUp.getCount());

                Warehouse warehouse = warehouseMapper.selectById(warehousingEntryUp.getWarehouseId());
                vo.setWarehouseId(warehousingEntryUp.getWarehouseId());
                vo.setWarehouseName(warehouse.getName());

                WarehouseArea warehouseArea = warehouseAreaMapper.selectById(warehousingEntryUp.getWarehouseAreaId());
                vo.setWarehouseAreaId(warehousingEntryUp.getWarehouseAreaId());
                vo.setWarehouseAreaName(warehouseArea.getName());

                WarehouseAreaLocal warehouseAreaLocal = warehouseAreaLocalMapper.selectById(warehousingEntryUp.getWarehouseAreaLocaId());
                vo.setWarehouseAreaLocaId(warehousingEntryUp.getWarehouseAreaLocaId());
                vo.setWarehouseAreaLocaCode(warehouseAreaLocal.getLocalCode());
                vos.add(vo);
            }
        });
        return new PageResult((long) vos.size(), vos);
    }
}
    